﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Configuration;
using System.Net;
using System.IO;
using Projectmanagement.Entity;

namespace Projectmanagement.Utl
{
    public class Email
    {
        public const string TaskEmail = "任务.html";
        public static bool SendTaskEmail(Task t)
        {

            var toNames = t.Owners.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            var toEmails = CH.DBAccount.EmployeeRoles.Where(w => toNames.Any(a => a == w.AccountName)).Select(s => s.Email);
            string toName = string.Join(";", toNames);
            string toEmail = string.Join(";", toEmails);
            #region Read template from file.
            string template = "";
            string file = AppDomain.CurrentDomain.BaseDirectory + "/App_Data/" + TaskEmail;
            if (System.IO.File.Exists(file))
            {
                FileInfo fi1einfo = new FileInfo(file);
                using (StreamReader sr = fi1einfo.OpenText())
                {
                    string s = "";
                    while ((s = sr.ReadLine()) != null)
                    {
                        template += s;
                    }
                }
            }
            string site = ConfigurationManager.AppSettings["PMSite"].ToString();

            template = template.Replace("{Owners}", t.Owners);
            template = template.Replace("{Name}", t.Name);
            template = template.Replace("{Content}", t.Content);
            template = template.Replace("{Links}", site);
            template = template.Replace("{ID}", t.ID.ToString());
            #endregion
            if (!string.IsNullOrEmpty(toEmail))
            {
                return SendEmail(toEmail, toName, "任务提醒：" + t.Name, template);
            }
            else
            {
                return false;
            }
        }

        public static bool SendEmail(string toEmail, string toName, string subject, string content)
        {


            // Send email
            SmtpClient smtp = new SmtpClient();
            smtp.Host = ConfigurationManager.AppSettings["SMTP_Server"].ToString();
            smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["SMTP_Port"].ToString());
            NetworkCredential SMTPUserInfo = new NetworkCredential(ConfigurationManager.AppSettings["SMTP_Username"].ToString(), ConfigurationManager.AppSettings["SMTP_Password"].ToString());
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = SMTPUserInfo;

            //MailAddress from = new MailAddress("system@cdmc.org.cn", "system@cdmc.org.cn", System.Text.Encoding.Unicode);
            MailAddress from = new MailAddress(SMTPUserInfo.UserName, "Task Remind", System.Text.Encoding.Unicode);
            MailAddress to = new MailAddress(toEmail, toName, System.Text.Encoding.UTF8);
            MailMessage message = new MailMessage(from, to);
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            message.Subject = subject;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.Body = content;
            message.IsBodyHtml = true;

            try
            {
                smtp.Send(message);
                return true;
            }
            catch
            {
                return false;
            }
            finally { message.Dispose(); smtp.Dispose(); }

        }
    }
}